package com.lingshi.shopping_goods_service.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lingshi.common.entity.Brand;
import com.lingshi.common.enums.BusCodeEnum;
import com.lingshi.common.exception.BusException;
import com.lingshi.common.service.IBrandService;
import com.lingshi.shopping_goods_service.mapper.BrandMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Objects;

@DubboService
public class BrandServiceImpl implements IBrandService {

    @Autowired
    private BrandMapper brandMapper;

    @Override
    public void add(Brand brand) {
        brandMapper.insert(brand);
    }

    @Override
    public void update(Brand brand) {
        brandMapper.updateById(brand);
    }

    @Override
    public void delete(Long id) {
        //判断是否商品关联
        //TODO 判断是否商品关联
        brandMapper.deleteById(id);
    }

    @Override
    public Page<Brand> search(Brand brand, int page, int size) {
        LambdaQueryWrapper<Brand> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(Objects.nonNull(brand) && StrUtil.isNotBlank(brand.getName())){
            lambdaQueryWrapper.like(Brand::getName,brand.getName());
        }
        Page<Brand> brandPage = brandMapper.selectPage(new Page<>(page, size), lambdaQueryWrapper);
        return brandPage;
    }

    @Override
    public Brand findById(Long id) {
        Brand brand = brandMapper.selectById(id);

        if(id < 1){
//            throw new BusException(BusCodeEnum.PARAMETER_ERROR);
            BusException.busException(BusCodeEnum.PARAMETER_ERROR);
        }

        return brand;
    }

    @Override
    public List<Brand> findAll() {
        return brandMapper.selectList(null);
    }
}
